import { combineReducers } from 'redux';
import { VisibilityFilters } from './actions';
const { SHOW_ALL } = VisibilityFilters;

function visibilityFilter(state = SHOW_ALL, action) {
    switch (action.type) {
        case SET_VISIBILITY_FILTER:
            return action.filter;
        default:
            return state;
    }
}

function todos(state = [], action) {
    switch (action.type) {
        case ADD_TODO:
            return [...state, {
                text: action.text,
                completed: false
            }];
        case COMPLETE_TODO:
            var idx = action.index;
            return [
                ...state.slice(0, idx),
                Object.assign({}, state[idx], {
                    completed: true
                }),
                ...state.slice(idx + 1)
            ];
        default:
            return state;
    }
}

/*
 function todoApp(state = {}, action) {
 /!*switch (action.type) {
 case SET_VISIBILITY_FILTER:
 return Object.assign({}, state, {
 visibilityFilter: action.filter
 });
 case ADD_TODO:
 case COMPLETE_TODO:
 return Object.assign({}, state, {
 todos: todos(state.todos, action)
 });
 default:
 return state;
 }*!/
 return {
 visibilityFilter: visibilityFilter(state.visibilityFilter, action),
 todos: todos(state.todos, action)
 }
 }
 */
const todoApp = combineReducers({
    visibilityFilter,
    todos
});

export default todoApp;